Java Database Programming with JDBC Java Database Programming with JDBC
by Pratik Patel
Coriolis, The Coriolis Group
ISBN: 1576100561   Pub Date: 10/01/96
  

Previous Table of Contents Next


The Bridge can only provide services for URLs that have a subprotocol of odbc. If a different subprotocol is given, the Bridge will simply tell the JDBC DriverManager that it has no idea what the URL means, and that it can’t support it. The subname specifies the ODBC data source name to use, followed by any additional connection string attributes. Here’s a code snippet that you can use to connect to an ODBC data source named Accounting, with a user name of dept12 and a password of Julie:

// Create a new instance of the JDBC-ODBC Bridge.

new jdbc.odbc.JdbcOdbcDriver();

// The JDBC-ODBC Bridge will have registered itself with the JDBC
// DriverManager. We can now let the DriverManager choose the right
// driver to connect to the given URL.

Connection con = DriverManager.getConnection("jdbc:odbc:Accounting",
   "dept12", "Julie");

An alternative way of connecting to this same data source would be to pass the user name and password as connection string attributes:

Connection con = DriverManager.getConnection("jdbc:odbc:Accounting;UID=
   dept12;PWD=Julie");

A third, more robust way of connecting would be to use a java.util.Properties object. DriverManager.getConnection is overloaded to support three versions of the interface:

public static synchronized Connection getConnection(String url, String
   user, String password) throws SQLException;
public static synchronized Connection getConnection(String url);
public static synchronized Connection getConnection(String url,
      java.util.Properties info);

The third method listed here is by far the most elegant way of connecting to any JDBC driver. An intelligent Java application/applet will use Driver.getPropertyInfo (which will not be covered here) to get a list of all of the required and optional properties for the driver. The Java program can then prompt the user for this information, and then create a java.util.Properties object that contains an element for each of the driver properties to be used for the JDBC connection. The following code shows how to setup the java.util.Properties object:

// Create the Properties object.

java.util.Properties prop = new java.util.Properties();

// Populate the Properties object with each property to be passed to the
// JDBC driver.

prop.put("UID", "dept12");
prop.put("PWD", "Julie");

Connection con = DriverManager.getConnection("jdbc:odbc:Accounting",
   prop);

JDBC To ODBC Calls: A Roadmap

For all of you ODBC junkies, Tables 5.1 through 5.8 show each JDBC method and the corresponding ODBC call (only JDBC methods that actually make an ODBC call are included). I can hear you now: “But isn’t this a closely guarded national secret? What if someone takes this information to write another Bridge?” First of all, the information provided here can be easily gathered by turning on the JDBC logging facility (DriverManager.setLogStream). The Bridge is nice enough to log every ODBC call as it is made, providing a log stream has been set via the DriverManager (all good JDBC drivers should provide adequate logging to aid in debugging). And second, the Bridge is provided for free. No one could possibly take this information to create a better Bridge at a lower price. It simply can’t be done. I provide this information in an effort to help you better understand how the Bridge operates, and, if you are well versed in ODBC, to give you the direct correlation between the Bridge and ODBC. This should enable you to write advanced JDBC applications right off the starting line.

Table 5.1 Driver ODBC calls.
JDBC Interface Method ODBC Call Comments
connect SQLDriverConnect The Bridge creates a connection string using the java.util. Properties attribute given
getPropertyInfo SQLBrowseConnect Each property returned is converted into a DriverPropertyInfo object

Table 5.2 Connection ODBC calls.
JDBC Interface Method ODBC Call Comments
prepareStatement SQLPrepare Prepares the statement for use with IN parameters
prepareCall SQLPrepare Prepares the statement for use with IN and OUT parameters (JDBC has not defined the use of IN/OUT parameters together)
nativeSQL SQLNativeSql Converts the given SQL into native format, expanding escape sequences
setAutoCommit SQLSetConnectOption fOption = SQL_AUTOCOMMIT
getAutoCommit SQLGetConnectOption fOption = SQL_AUTOCOMMIT
commit SQLTransact fType = SQL_COMMIT
rollback SQLTransact fType = SQL_ROLLBACK
close SQLFreeConnect Frees the connection handle associated with the connection
setReadOnly SQLSetConnectOption fOption = SQL_ACCESS_MODE; this is only a hint to the ODBC driver; the underlying driver may not actually change its behavior
isReadOnly SQLGetConnectOption fOption = SQL_ACCESS_MODE
setCatalog SQLSetConnectOption fOption = SQL_CURRENT_
QUALIFIER
getCatalog SQLGetInfo fInfoType = SQL_DATABASE_NAME
setTransactionIsolation SQLSetConnectOption fOption = SQL_TXN_ISOLATION
getTransactionIsolation SQLGetConnectOption fOption = SQL_TXN_ISOLATION
setAutoClose   ODBC does not provide a method to modify this behavior
getAutoClose SQLGetInfo fInfoType = SQL_CURSOR_COMMIT_
BEHAVIOR and fInfoType = SQL_CURSOR_
ROLLBACK_BEHAVIOR; the Bridge makes both calls, and if either are true, then getAutoClose returns true


Previous Table of Contents Next